//
//  HZOrderView.m
//  Stock_Stategy
//
//  Created by MayerF on 2018/1/22.
//  Copyright © 2018年 MayerF. All rights reserved.
//

#import "HZOrderView.h"
#import "HZOrderDownView.h"

@interface HZOrderView ()<DatePickerViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong)UIScrollView *pScrollView;
@property (nonatomic, strong)UIView *pConstraintView;
@property (nonatomic, strong)UIView *pTopView;
@property (nonatomic, strong)UIView *pTop2View;
@property (nonatomic, strong)UIView *pMiddelView;
@property (nonatomic, strong)UILabel *pTopLabel;
@property (nonatomic, strong)UIButton *pPriceBtn;
@property (nonatomic, strong)UIButton *pTimeBtn;
@property (nonatomic, strong)UILabel *pDirectionLable;
@property (nonatomic, strong)UIButton *pBetterBtn;
@property (nonatomic, strong)UIButton *pSmatterBtn;
@property (nonatomic, strong)UILabel *pPriceLabel;
@property (nonatomic, strong)UITextField *pActiveDate;
@property (nonatomic, strong)UITextField *pOrderPrice;
@property (nonatomic, strong)HZOrderDownView *pDownView;
@property (nonatomic, strong)UIView *pBottomView;
@property (nonatomic, strong)UIButton *pReadProtocol;
@property (nonatomic, strong)UIButton *pProtocol;
@property (nonatomic, strong)UIButton *pAddBtn;
@property (nonatomic, strong)UILabel *pWords;
@property (nonatomic, strong)CAShapeLayer *pShapeLayer;
@property (nonatomic, strong)DatePickerView   *pDatePickerView;

@property (nonatomic, copy)NSString *pLastPrice;
@property (nonatomic, strong)UITextField *pCurrentTextField;
@end

@implementation HZOrderView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pScrollView = [UIScrollView new];
    _pConstraintView = [UIView new];
    _pTopView = [UIView new];
    _pTopLabel = [UILabel new];
    _pPriceBtn = [UIButton new];
    _pTimeBtn = [UIButton new];
    _pTop2View = [UIView new];
    _pDirectionLable = [UILabel new];
    _pPriceLabel = [UILabel new];
    _pBetterBtn = [UIButton new];
    _pSmatterBtn = [UIButton new];
    _pOrderPrice = [UITextField new];
    _pDownView = [HZOrderDownView new];
    _pBottomView = [UIView new];
    _pReadProtocol = [UIButton new];
    _pProtocol = [UIButton new];
    _pAddBtn = [UIButton new];
    _pActiveDate = [UITextField new];
    
    _pDatePickerView = [DatePickerView new];
    _pDatePickerView.delegete = self;
    _pDatePickerView.hidden = YES;
    
    _pScrollView.bounces = NO;
    _pBottomView.backgroundColor = [UIColor whiteColor];
    _pTopView.backgroundColor = [UIColor whiteColor];
    _pTop2View.backgroundColor = [UIColor whiteColor];
    _pOrderPrice.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    _pTopLabel.textColor = kMYHexToRGB(0x999999, 1);
    _pDirectionLable.textColor = kMYHexToRGB(0x999999, 1);
    _pPriceLabel.textColor = kMYHexToRGB(0x999999, 1);
    _pOrderPrice.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 50)];
    _pOrderPrice.leftViewMode = UITextFieldViewModeAlways;
    _pActiveDate.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 50)];
    _pActiveDate.leftViewMode = UITextFieldViewModeAlways;
    _pActiveDate.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    
    _pAddBtn.layer.cornerRadius = 5;
    _pAddBtn.layer.masksToBounds = YES;
    
    _pActiveDate.delegate = self;
    _pOrderPrice.delegate = self;
    _pPriceBtn.selected = YES;
    _pBetterBtn.selected = YES;
    _pTopLabel.text = @"条件类型";
    _pDirectionLable.text = @"触发方向";
    _pPriceLabel.text = @"触发价格";
    _pOrderPrice.placeholder = @"条件单触发价";
    _pActiveDate.placeholder = @"请输入触发日期";
    _pActiveDate.hidden = YES;
    _pPriceBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _pTimeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _pBetterBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _pSmatterBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _pPriceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _pTimeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _pBetterBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _pSmatterBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_pTimeBtn setTitle:@"时点" forState:UIControlStateNormal];
    [_pPriceBtn setTitle:@"价格" forState:UIControlStateNormal];
    [_pBetterBtn setTitle:@"大于等于" forState:UIControlStateNormal];
    [_pSmatterBtn setTitle:@"小于等于" forState:UIControlStateNormal];
    [_pPriceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_pPriceBtn setImage:[UIImage imageNamed:@"zc_xy"] forState:UIControlStateNormal];
    [_pPriceBtn setImage:[UIImage imageNamed:@"zc_xy_click"] forState:UIControlStateSelected];
    [_pTimeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_pTimeBtn setImage:[UIImage imageNamed:@"zc_xy"] forState:UIControlStateNormal];
    [_pTimeBtn setImage:[UIImage imageNamed:@"zc_xy_click"] forState:UIControlStateSelected];
    [_pBetterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_pBetterBtn setImage:[UIImage imageNamed:@"zc_xy"] forState:UIControlStateNormal];
    [_pBetterBtn setImage:[UIImage imageNamed:@"zc_xy_click"] forState:UIControlStateSelected];
    [_pSmatterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_pSmatterBtn setImage:[UIImage imageNamed:@"zc_xy"] forState:UIControlStateNormal];
    [_pSmatterBtn setImage:[UIImage imageNamed:@"zc_xy_click"] forState:UIControlStateSelected];
    [_pPriceBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    [_pTimeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    [_pBetterBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    [_pSmatterBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    [_pPriceBtn addTarget:self action:@selector(priceBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [_pTimeBtn addTarget:self action:@selector(timeBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [_pBetterBtn addTarget:self action:@selector(betterBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [_pSmatterBtn addTarget:self action:@selector(smallerBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pReadProtocol.titleLabel.font = [UIFont systemFontOfSize:15];
    [_pReadProtocol setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 0)];
    [_pReadProtocol setImage:[UIImage imageNamed:@"zc_xy"] forState:UIControlStateNormal];
    [_pReadProtocol setImage:[UIImage imageNamed:@"zc_xy_click"] forState:UIControlStateSelected];
    [_pReadProtocol setTitle:@"我已阅读并同意" forState:UIControlStateNormal];
    [_pReadProtocol setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_pReadProtocol addTarget:self action:@selector(readBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pProtocol.titleLabel.font = [UIFont systemFontOfSize:15];
    [_pProtocol setTitle:@"《合作协议》" forState:UIControlStateNormal];
    [_pProtocol setTitleColor:kMYHexToRGB(0x3399ff, 1) forState:UIControlStateNormal];
    [_pProtocol addTarget:self action:@selector(protocolBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pAddBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    _pAddBtn.backgroundColor = kREDColor;
    [_pAddBtn setTitle:@"添加" forState:UIControlStateNormal];
    [_pAddBtn addTarget:self action:@selector(addBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pWords = [UILabel new];
    _pWords.numberOfLines = 0;
    _pWords.font = [UIFont systemFontOfSize:13];
    _pWords.textColor = kMYHexToRGB(0xbbbbbb, 1);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"特别提示：\n本页所述条件单皆为（创建策略）条件单\n1.交易日14:45:00-15:00:00不能添加条件单;\n2.交易日14:45:00之前创建的条件单，当天14:49:00之前有效；\n3.交易日15:00:00之后或非交易日创建的条件单下个交易日14:49:00之前有效;\n4.时点条件单中的触发时点必须在9:30-11:29:00或13:00:00-14:49:00之内；\n5.交易日14:45:00之前创建的时点条件单，条件中的触发日期必须是当日，触发时点必须晚于下单时点；\n6.交易日15:00:00之后或非交易日创建的时点条件单，条件中的触发日期必须是下个交易日。"];
    
    NSRange range = [attStr.string rangeOfString:@"充值额度由您 所持银行卡的网上支付额度，以及银行配置给第三方支付平台的额度来决定"];
    
    [attStr addAttribute:NSForegroundColorAttributeName value:kNavColor range:range];
    _pWords.attributedText = attStr;
    
    _pShapeLayer = [CAShapeLayer layer];
    
}
- (void)initConstraints
{
    [self addSubview:_pScrollView];
    [_pScrollView addSubview:_pConstraintView];
    [_pConstraintView addSubview:_pTopView];
    [_pTopView addSubview:_pTopLabel];
    [_pTopView addSubview:_pPriceBtn];
    [_pTopView addSubview:_pTimeBtn];
    [_pConstraintView addSubview:_pTop2View];
    [_pTop2View addSubview:_pDirectionLable];
    [_pTop2View addSubview:_pBetterBtn];
    [_pTop2View addSubview:_pSmatterBtn];
    [_pTop2View addSubview:_pPriceLabel];
    [_pTop2View addSubview:_pOrderPrice];
    [_pTop2View addSubview:_pActiveDate];
    [_pConstraintView addSubview:_pDownView];
    [_pConstraintView addSubview:_pBottomView];
    [_pBottomView addSubview:_pReadProtocol];
    [_pBottomView addSubview:_pProtocol];
    [_pBottomView addSubview:_pAddBtn];
    [_pBottomView addSubview:_pWords];
    [self addSubview:_pDatePickerView];
    
    [_pScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    [_pConstraintView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_pScrollView);
        make.width.mas_equalTo(self);
        make.bottom.mas_equalTo(_pBottomView);
    }];
    [_pTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(_pConstraintView);
        make.height.mas_equalTo(50);
    }];
    [_pTopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pTopView);
        make.left.mas_equalTo(_pTopView).offset(15);
        make.right.mas_equalTo(_pPriceBtn.mas_left);
    }];
    [_pPriceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pTopView);
        make.right.mas_equalTo(_pTimeBtn.mas_left);
    }];
    [_pTimeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pTopView);
        make.right.mas_equalTo(_pTopView).offset(-15);
        make.width.mas_equalTo(@[_pTopLabel, _pPriceBtn]);
    }];
    [_pTop2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTopView.mas_bottom).offset(10);
        make.left.right.mas_equalTo(_pTopView);
        make.height.mas_equalTo(50*2);
    }];
    [_pDirectionLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTop2View);
        make.left.mas_equalTo(_pTop2View).offset(15);
        make.right.mas_equalTo(_pBetterBtn.mas_left);
        make.height.mas_equalTo(50);
    }];
    [_pBetterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pDirectionLable);
        make.right.mas_equalTo(_pSmatterBtn.mas_left);
    }];
    [_pSmatterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pDirectionLable);
        make.right.mas_equalTo(_pTop2View).offset(-15);
        make.width.mas_equalTo(@[_pDirectionLable, _pBetterBtn]);
    }];
    [_pPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pDirectionLable.mas_bottom);
        make.left.mas_equalTo(_pDirectionLable);
        make.bottom.mas_equalTo(_pTop2View);
        make.right.mas_equalTo(_pOrderPrice.mas_left);
    }];
    [_pActiveDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pDirectionLable).offset(5);
        make.bottom.mas_equalTo(_pDirectionLable).offset(-10);
        make.right.mas_equalTo(_pTop2View).offset(-15);
        make.width.mas_equalTo(_pPriceLabel).multipliedBy(2);
    }];
    [_pOrderPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pPriceLabel).offset(5);
        make.bottom.mas_equalTo(_pPriceLabel).offset(-10);
        make.right.mas_equalTo(_pTop2View).offset(-15);
        make.width.mas_equalTo(_pPriceLabel).multipliedBy(2);
    }];
    
    [_pDownView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTop2View.mas_bottom).offset(10);
        make.left.right.mas_equalTo(_pConstraintView);
    }];
    
    [_pBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pDownView.mas_bottom).offset(10);
        make.left.right.mas_equalTo(_pConstraintView);
        make.height.mas_equalTo(400);
    }];
    [_pReadProtocol mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pBottomView);
        make.left.mas_equalTo(_pBottomView).offset(10);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(35);
    }];
    [_pProtocol mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pReadProtocol);
        make.left.mas_equalTo(_pReadProtocol.mas_right);
        make.width.mas_equalTo(95);
    }];
    [_pAddBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_pConstraintView);
        make.centerY.mas_equalTo(_pReadProtocol.mas_bottom).offset(20+20);
        make.width.mas_equalTo(180);
        make.height.mas_equalTo(40);
    }];
    
    [_pWords mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pAddBtn.mas_bottom).offset(15);
        make.left.mas_equalTo(self).offset(10);
        make.right.mas_equalTo(self).offset(-10);
        make.height.mas_equalTo(230);
    }];
    
    [_pDatePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(260);
    }];
    
}

// 获取日期
- (NSString *)getCurrentDate
{
    NSDate *pNowTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    NSString *pTime = [formatter stringFromDate:pNowTime];
    return pTime;
}
// 获取时间
- (NSString *)getCurrentTime
{
    NSDate *pNowTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:00:00"];
    NSString *pTime = [formatter stringFromDate:pNowTime];
    return pTime;
}

// switch
- (void)switchTypeWithisPrice:(BOOL)isPrice
{
    if (isPrice) {
        _pDirectionLable.text = @"触发方向";
        _pPriceLabel.text = @"触发价格";
        _pActiveDate.hidden = YES;
        _pBetterBtn.hidden = _pSmatterBtn.hidden = NO;
        _pOrderPrice.placeholder = _pLastPrice;
    }else {
        _pDirectionLable.text = @"触发日期";
        _pPriceLabel.text = @"触发时点";
        _pActiveDate.hidden = NO;
        _pBetterBtn.hidden = _pSmatterBtn.hidden = YES;
        _pActiveDate.placeholder = [self getCurrentDate];
        _pOrderPrice.placeholder = [self getCurrentTime];
    }
}

- (void)readBtnOnClick
{
    _pReadProtocol.selected = !_pReadProtocol.selected;
}
- (void)protocolBtnOnClick
{
//    NSDictionary *dict = [_pMoneyView getPotocolParas];
//    LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterHomeLearning];
//    if (block) {
//        block(dict, nil);
//    }
}
- (void)priceBtnOnClick
{
    if (!_pPriceBtn.selected) {
        _pTimeBtn.selected = NO;
        _pPriceBtn.selected = !_pPriceBtn.selected;
        _pOrderPrice.text = _pLastPrice;
        [self switchTypeWithisPrice:YES];
    }
}
- (void)timeBtnOnClick
{
    if (!_pTimeBtn.selected) {
        _pPriceBtn.selected = NO;
        _pTimeBtn.selected = !_pTimeBtn.selected;
        [self switchTypeWithisPrice:NO];
        [self endEditing:YES];
    }
}
- (void)betterBtnOnClick
{
    if (!_pBetterBtn.selected) {
        _pSmatterBtn.selected = NO;
        _pBetterBtn.selected = !_pBetterBtn.selected;
    }
}
- (void)smallerBtnOnClick
{
    if (!_pSmatterBtn.selected) {
        _pBetterBtn.selected = NO;
        _pSmatterBtn.selected = !_pSmatterBtn.selected;
    }
}
- (void)addBtnOnClick
{
    NSString *type = @"1";
    NSString *direction = @"1";
    NSString *price = @"";
    NSString *time = @"";
    if (_pTimeBtn.selected) {
        type = @"2";
        direction = @"";
        NSString *temp = _pActiveDate.text;
        if ([temp isEqual:@""]) {
            temp = [self getCurrentTime];
        }
        time = temp;
    }
    if (_pPriceBtn.selected) {
        if (_pSmatterBtn.selected) {
            direction = @"2";
        }
        NSString *temp = _pOrderPrice.text;
        if ([temp isEqual:@""]) {
            temp = _pOrderPrice.placeholder;
        }
        price = temp;
    }
    NSDictionary *dict = @{@"type":type, @"direction":direction, @"price":price,@"time":time};
    
    LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterAddOrder];
    if (block) {
        block(dict, @(_pReadProtocol.selected));
    }
}

- (void)updateData:(NSArray *)data price:(NSString *)price
{
    _pLastPrice = price;
    [_pDownView updateNewData:data];
    _pOrderPrice.placeholder = price;
    
//    _pShapeLayer = [CAShapeLayer layer];
//    _pShapeLayer.frame = _pAddBtn.bounds;
//    UIBezierPath *pbezierPath = [UIBezierPath bezierPathWithRoundedRect:_pAddBtn.bounds cornerRadius:5];
//    _pShapeLayer.path = pbezierPath.CGPath;
//    _pAddBtn.layer.mask = _pShapeLayer;
}
- (void)updateName:(NSArray *)names
{
    [_pDownView updateName:names];
}
- (void)clearData
{
    _pOrderPrice.text = @"";
    _pActiveDate.text = @"";
    if (_pPriceBtn.selected) {
        _pOrderPrice.placeholder = _pLastPrice;
    }else {
        _pOrderPrice.placeholder = [self getCurrentTime];
    }
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    _pDatePickerView.hidden = NO;
    [self bringSubviewToFront:_pDatePickerView];
    if (_pPriceBtn.selected) {
        _pDatePickerView.hidden = YES;
        return YES;
    }
    _pCurrentTextField = textField;
    if ([textField isEqual:_pActiveDate]) {
        [_pDatePickerView setModeWithMode:UIDatePickerModeDate];
    }else {
        [_pDatePickerView setModeWithMode:UIDatePickerModeTime];
    }
    return NO;
}
#pragma mark -
#pragma mark DatePickerViewDelegate
- (void)datePickerClickWithDate:(NSString *)date {
    _pCurrentTextField.text = date;
}

@end
